import logging

import pytest

from Keys.api_key import ApiKey


@pytest.fixture(scope="session")
def send_token():
    ak = ApiKey()
    res = ak.post(url="http://127.0.0.1:8888/api/front/user/login",
                  json={"username": "18855688009", "password": "xx958012725"},
                  headers={"Content-Type": "application/json"})
    res_data = res.json()
    token = res_data["data"]["token"]
    yield token

# 获取验证码sessionId
# @pytest.fixture(scope="session")
# def send_sessionId():
#     ak = ApiKey()
#     res = ak.get(url="http://127.0.0.1:8888/api/front/resource/img_verify_code",
#                   headers={"Content-Type": "application/json"})
#     res_data = res.json()
#     sessionId = res_data["data"]["sessionId"]
#     yield sessionId


# 设置日志打印级别
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
def pytest_runtest_makereport(item, call):
    out = yield
    res = out.get_result()
    if res.when == "call":
        logging.info(f"用例ID：{res.nodeid}")
        logging.info(f"测试结果：{res.outcome}")
        logging.info(f"故障表示：{res.longrepr}")
        logging.info(f"异常：{call.excinfo}")
        logging.info(f"用例耗时：{res.duration}")
        logging.info("*******************************************")


